//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "stdafx.h"
#include "GlobalVariables.h"
#include "FormMotionControlInterface.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "YbCommDevice"
#pragma resource "*.dfm"
TFormMotionControl *FormMotionControl;
//---------------------------------------------------------------------------
__fastcall TFormMotionControl::TFormMotionControl(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMotionControl::FormCreate(TObject *Sender)
{
	FormMotionControl->Top = 150;
	FormMotionControl->Left = 150;
	Button_MainSettingEnabledClick(NULL);

	InitialSetting();
	CheckPortStatus();

	if(TimerMain.Enabled == true)
	{
		Button_MainProcessTimerSwitch->Caption =  "Click to OFF";
	}
	else
	{
		Button_MainProcessTimerSwitch->Caption =  "Click to ON";
	}

	#if defined ADULT
		FormMotionControl->Caption = "Motion Control Interface for Adult";
	#elif defined TEEN

		FormMotionControl->Caption = "Motion Control Interface for Teen";
	#elif defined KID
		FormMotionControl->Caption = "Motion Control Interface for Kid";

	#endif
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Comm_SettingClick(TObject *Sender)
{
	if(Sender == CommUB_Setting)
	{
		Comm_UB->SettingsDialog(FormMotionControl, false);
	}
	else if(Sender == CommRL_Setting)
	{
		Comm_RL->SettingsDialog(FormMotionControl, false);
	}
	else if(Sender == CommLL_Setting)
	{
		Comm_LL->SettingsDialog(FormMotionControl, false);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Comm_Enable(TObject *Sender)
{
	if(Sender == CommUB_Open || Sender == Comm_Open)
	{
		Comm_UB->Active = true;
		Memo_ShowInfo->Lines->Add("Open Comm UB: Port" + IntToStr(Comm_UB->PortNo));
	}
	if(Sender == CommRL_Open || Sender == Comm_Open)
	{
		Comm_RL->Active = true;
		Memo_ShowInfo->Lines->Add("Open Comm RL: Port" + IntToStr(Comm_RL->PortNo));
	}
	if(Sender == CommLL_Open || Sender == Comm_Open)
	{
		Comm_LL->Active = true;
		Memo_ShowInfo->Lines->Add("Open Comm LL: Port" + IntToStr(Comm_LL->PortNo));
	}
	if(Sender == CommUB_Close || Sender == Comm_Close)
	{
		Comm_UB->Active = false;
		Memo_ShowInfo->Lines->Add("Close Comm UB: Port" + IntToStr(Comm_UB->PortNo));
	}
	if(Sender == CommRL_Close || Sender == Comm_Close)
	{
		Comm_RL->Active = false;
		Memo_ShowInfo->Lines->Add("Close Comm RL: Port" + IntToStr(Comm_RL->PortNo));
	}
	if(Sender == CommLL_Close || Sender == Comm_Close)
	{
		Comm_LL->Active = false;
		Memo_ShowInfo->Lines->Add("Close Comm LL: Port" + IntToStr(Comm_LL->PortNo));
	}
  CheckPortStatus();
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Memo_ShowInfoDblClick(TObject *Sender)
{
	Memo_ShowInfo->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::FormDestroy(TObject *Sender)
{
	delete MOTOR_UB_PORT;
	delete MOTOR_RL_PORT;
	delete MOTOR_LL_PORT;
	delete Comm_Comp;
	delete Comm_HMI;
	delete Comm_ControlBoard;
	if(TimerMain.Enabled)
		TimerStop(&TimerMain);
	if(TimerSub.Enabled)
		TimerStop(&TimerSub);
	TimerEndPeriod();
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Button_PortSettingDialogClick(TObject *Sender)
{
	if(Sender == Button_CompSettingDialog)
	{
		COMP_PORT->SettingsDialog(FormMotionControl, false);
	}
	else if(Sender == Button_HMISettingDialog)
	{
		HMI_PORT->SettingsDialog(FormMotionControl, false);
	}
	else if(Sender == Button_BoardSettingDialog)
	{
		BOARD_PORT->SettingsDialog(FormMotionControl, false);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Button_PortSwitchClick(TObject *Sender)
{
	TYbCommDevice *comm_temp = NULL;
	TButton *button = ((TButton *)Sender);
	if(Sender == Button_CompPortSwitch)
	{
		comm_temp = COMP_PORT;
	}
	else if(Sender == Button_HMIPortSwitch)
	{
		comm_temp = HMI_PORT;
	}
	else if(Sender == Button_BoardPortSwitch)
	{
		comm_temp = BOARD_PORT;
	}

	if(comm_temp != NULL)
	{
		comm_temp->Active = !comm_temp->Active;
		if(comm_temp->Active)
		{
			button->Caption = "Close Port";
		}
		else
		{
			button->Caption = "Open Port";
		}
	}
	CheckPortStatus();
}
//---------------------------------------------------------------------------

void TFormMotionControl::CheckPortStatus(void)
{
	AnsiString sUB = "";
	AnsiString sRL = "";
	AnsiString sLL = "";
	AnsiString sComp = "";
	AnsiString sHMI = "";
	AnsiString sBoard = "";
	sUB = (Comm_UB->Active ? "ON" : "OFF");
	sRL = (Comm_RL->Active ? "ON" : "OFF");
	sLL = (Comm_LL->Active ? "ON" : "OFF");
	sComp = (COMP_PORT->Active ? "ON" : "OFF");
	sHMI = (HMI_PORT->Active ? "ON" : "OFF");
	sBoard = (BOARD_PORT->Active ? "ON" : "OFF");
	StatusBarMain->Panels->Items[0]->Text = "UB: " + sUB + " | RL: " + sRL + " | LL: " +sLL +
																					" | Comp: " + sComp + " | HMI: " + sHMI + " | Board: " + sBoard;
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Button_MainSettingEnabledClick(TObject *Sender)
{
	bool enabled = !RadioGroup_CompSettings->Enabled;
	RadioGroup_CompSettings->Enabled = enabled;
	RadioGroup_HMISettings->Enabled = enabled;
	Button_CompSettingDialog->Enabled = enabled;
	Button_HMISettingDialog->Enabled = enabled;
	Button_BoardSettingDialog->Enabled = enabled;
	Button_CompPortSwitch->Enabled = enabled;
	Button_HMIPortSwitch->Enabled = enabled;
	Button_BoardPortSwitch->Enabled = enabled;
  Button_MainProcessTimerSwitch->Enabled = enabled;
	if(enabled)
	{
    Button_MainSettingEnabled->Caption = "Disable";
	}
	else
	{
    Button_MainSettingEnabled->Caption = "Enable";
	}
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Button_ShowInfoClick(TObject *Sender)
{
	FormShowInfo->Show();
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::RadioGroup_CompSettingsClick(TObject *Sender)
{
	if(RadioGroup_CompSettings->ItemIndex == 0)
	{
		bCommunicateCompWithShareMemory = true;
		Memo_ShowInfo->Lines->Add("C Share memory");
	}
	else
	{
		bCommunicateCompWithShareMemory = false;
		Memo_ShowInfo->Lines->Add("C RS232");
  }
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::RadioGroup_HMISettingsClick(TObject *Sender)
{
	if(RadioGroup_HMISettings->ItemIndex == 0)
	{
		bCommunicateHMIWithShareMemory = true;
		Memo_ShowInfo->Lines->Add("H Share memory");
	}
	else
	{
		bCommunicateHMIWithShareMemory = false;
		Memo_ShowInfo->Lines->Add("H RS232");
  }
}
//---------------------------------------------------------------------------


void __fastcall TFormMotionControl::Button_MainProcessTimerSwitchClick(TObject *Sender)
{
	if(TimerMain.Enabled == true)
	{
		TimerStop(&TimerMain);
		TimerStop(&TimerSub);
		Button_MainProcessTimerSwitch->Caption =  "Click to ON";
	}
	else
	{
		TimerStart(&TimerMain);
		TimerStart(&TimerSub);
		Button_MainProcessTimerSwitch->Caption =  "Click to OFF";
  }
}
//---------------------------------------------------------------------------

void __fastcall TFormMotionControl::Button1Click(TObject *Sender)
{
	for(int i=0; i < 25; i++)
	{
		Memo_ShowInfo->Lines->Add("ID: " + IntToStr(i)+ ", " +
															ID2Index(i));
  }
}
//---------------------------------------------------------------------------

